<?php

namespace app\common\model;

use think\Model;

class GoodsComment extends Model
{
    // 表名
    protected $name = 'goods_comment';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'score_text',
        'create_time_text',
        'update_time_text',
        'is_anonymous_text',
        'reply_time_text'
    ];
    

    
    public function getScoreList()
    {
        return ['1' => __('1'),'2' => __('2'),'3' => __('3'),'4' => __('4'),'5' => __('5')];
    }     

    public function getIsAnonymousList()
    {
        return ['1' => '是', '0' => '否'];
    }     


    public function getScoreTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['score'];
        $list = $this->getScoreList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsAnonymousTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['is_anonymous'];
        $list = $this->getIsAnonymousList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getReplyTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['reply_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setReplyTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function user(){
    	return $this->belongsTo('User', 'user_id', 'id','', 'left')->setEagerlyType(0);
    }
    
    public function goods(){
    	return $this->hasOne('Goods', 'id', 'goods_id', '', 'left')->setEagerlyType(0);
    }
	
	public function order()
	{
		return $this->hasOne('Order', 'id', 'order_id', '', 'left')->setEagerlyType(0);
	}
}
